package com.tiantian.quanminxiu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.adapter.SearchListAdapter;
import com.tiantian.quanminxiu.dialog.Dialog_EditName;
import com.tiantian.quanminxiu.dialog.Dialog_EditSex;
import com.tiantian.quanminxiu.model.RoomInfo;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.EditInfoPresent;
import com.tiantian.quanminxiu.presenter.SearchPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.CommonEnum;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.EditInfoView;
import com.tiantian.quanminxiu.view.SearchView;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/12.
 */
public class Activity_Search extends ActivityBase implements SearchView {
    //region 初始化数据
    private SearchPresent searchPresent;
    private TextView txtSearchResult;
    private View linContentSearch,linHotSearch;
    private RecyclerView mRecyclerView,mResultRecyclerView;
    private SearchListAdapter searchListAdapter,resultListAdapter;
    //endregion

    //region onCreateView
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Init();
        InitView();

        searchPresent = new SearchPresent();
        searchPresent.attachView(this);

        UserData.Reload.Activity_Search = true;
    }
    //endregion

    //region InitView()
    public void InitView() {

        mHeader.etSearchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,KeyEvent event)  {
                if (actionId== EditorInfo.IME_ACTION_SEARCH) {
                    if(TextUtils.isEmpty(mHeader.etSearchContent.getText())){
                        CommonUtils.showToask(thisContext,"查询内容为空..");
                        hideKeyboard(thisContext);
                        return false;
                    };
                    searchPresent.getSearchResult(mHeader.etSearchContent.getText().toString());
                    hideKeyboard(thisContext);
                    return true;
                }
                return false;
            }
        });

        linContentSearch =  findViewById(R.id.linContentSearch);
        linHotSearch =  findViewById(R.id.linHotSearch);
        txtSearchResult = (TextView) findViewById(R.id.txtSearchResult);

        mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
        mResultRecyclerView = (RecyclerView) findViewById(R.id.mResultRecyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(thisContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        LinearLayoutManager resultlinearLayoutManager = new LinearLayoutManager(thisContext);
        resultlinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mResultRecyclerView.setLayoutManager(resultlinearLayoutManager);

        searchListAdapter = new SearchListAdapter(thisActivity);
        mRecyclerView.setAdapter(searchListAdapter);

        resultListAdapter = new SearchListAdapter(thisActivity);
        mResultRecyclerView.setAdapter(resultListAdapter);
    }
    //endregion

    //region OC_Button
    public Action1<View> OC_Button = v->{
    };
    //endregion

    //region PageInit
    public void PageInit() {
        if (!UserData.Reload.Activity_Search) {
            return;
        }
        UserData.Reload.Activity_Search = false;

        searchPresent.getHotSearch();
    }
    //endregion

    //region onResume onDestroy
    @Override
    public void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        searchPresent.detachView();
    }
    //endregion

    //region 得到热门搜索列表 getHotSearch(List<RoomInfo> list)
    @Override
    public void getHotSearch(List<RoomInfo> list) {
        searchListAdapter.getData().clear();
        searchListAdapter.setNewData(list);
    }
    //endregion

    //region 得到搜索结果列表 getSearchResult(List<RoomInfo> list)
    @Override
    public void getSearchResult(List<RoomInfo> list) {
        linContentSearch.setVisibility(View.VISIBLE);
        linHotSearch.setVisibility(View.GONE);
        if (list.size()==0){
            txtSearchResult.setText("搜索结果为空");
        }else {
            txtSearchResult.setText("搜索结果");
        }
        resultListAdapter.getData().clear();
        resultListAdapter.setNewData(list);
    }
    //endregion

    //region 隐藏软键盘 hideKeyboard
    public void hideKeyboard(Context context) {
        Activity activity = (Activity) context;
        if (activity != null) {
            InputMethodManager imm = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive() && activity.getCurrentFocus() != null) {
                imm.hideSoftInputFromWindow(activity.getCurrentFocus()
                        .getWindowToken(), 0);
            }
        }
    }
    //endregion
}
